<template>
	<view>
		<baseForm :column.sync="column" @submitFun="submitFun" @changeColumn="changeColumn" :submitStatus="submitDisable"></baseForm>
	</view>
</template>

<script>
	import baseForm from '@/wxcomponents/form/form.vue';
	import formMixins from '@/libs/formMixins.js';
	import {
		format
	} from '@/libs/common.js';
	import login from '@/libs/login.js';
	import dictMixins from '@/libs/dictMixins.js';
	export default {
		mixins: [formMixins, dictMixins],
		data() {
			let self = this;
			return {
				column: [{
						label: '停课起始日期',
						name: 'suspendStart',
						type: 'picker',
						mode: 'date',
						disabled: false,
						required: true,
						value: format('YY-mm-dd', new Date()),
						start: format('YY-mm-dd', new Date()),
						text: format('YY-mm-dd', new Date())
					},
					{
						label: '停课结束日期',
						name: 'suspendEnd',
						type: 'picker',
						mode: 'date',
						disabled: false,
						required: true,
						value: '',
						start: format('YY-mm-dd', new Date()),
						text: '请选择'
					},
					{
						label: '停课天数',
						name: 'suspendDay',
						type: 'normalInput',
						disabled: true,
						required: true,
						value: ''
					},
					{
						label: '停课年级',
						name: 'grade',
						type: 'picker',
						mode: 'selector',
						required: true,
						disabled: false,
						value: '',
						text: '请选择',
						range: self.findOneDictList('18000')
					},
					{
						label: '停课专业',
						name: 'majorCode',
						type: 'picker',
						mode: 'selector',
						required: true,
						disabled: false,
						value: '',
						text: '请选择',
						range: []
					},
					{
						label: '停课班级',
						name: 'classCode',
						type: 'picker',
						mode: 'selector',
						disabled: false,
						required: true,
						value: '',
						text: '请选择',

						range: []
					},
					{
						label: '停课原因',
						name: 'suspendReason',
						type: 'normalInput',
						disabled: false,
						required: true,
						value: ''
					}
				]
			};
		},
		async onLoad() {
			this.$store.state.idsObj=[]
			this.updateParamsWithCash();
			this.initData();
			// let resp = this.findOneDictList('dict_course_change_type')
			let resp = await this.getInfo()
			this.column[4].range = resp.map(item => {
				return {
					name: item.majorName,
					value: item.majorCode
				}
			})
		},
		onShow() {},
		onHide() {
			this.savePageTempParams({
				name: 'changeCoruse',
				formModel: this.formModel,
				syncColumn: this.syncColumn
			});
		},
		methods: {
			//年级班级专业联动查询
			async getInfo(queryType = 2, classCode = '', majorCode = '', grade = '') {
				let params = {
					grade: grade, //年级
					classCode: classCode, //班级
					majorCode: majorCode, //专业
					queryType: queryType
				};
				let resp = await this.$http.post({
					url: '/stu/common/gradeMajorClassList',
					data: params
				});
				return resp.result;
			},
			beforeTimeIndexCal: async function() {
				let that = this;
				let columnObj = this.transColumnToObj(this.column);
				if (columnObj.beforeDate == '' || columnObj.classCode == '') return;
				let user = await login.getStorage('user');
				let params = {
					jobNumber: user.number,
					courseDate: columnObj.beforeDate,
					classCode: columnObj.classCode,
					courseCode: columnObj.courseCode
				};
				let resp = await that.$http.post({
					url: '/edu/classCourseChange/getClassAndWeekDayIndex',
					data: params
				});
				// that.column[5].value = resp.result.afterTimeIndex;
				this.fetchDataToColumn('beforeTimeIndex', resp.result.beforeTimeIndex)
				return resp.result.beforeTimeIndex;
			},
			afterTimeIndexCal: async function() {
				let that = this;
				let columnObj = this.transColumnToObj(this.column);
				let params = {
					jobNumber: columnObj.afterJobNumber,
					courseDate: columnObj.afterDate,
					classCode: columnObj.classCode,
					courseCode: columnObj.afterCourseCode
				};
				if (params.jobNumber == '' || params.courseDate == '' || params.classCode == '') return;
				let resp = await that.$http.post({
					url: '/edu/classCourseChange/getClassAndWeekDayIndex',
					data: params
				});
				// this.fetchDataToColumn('afterTimeIndex',resp.result.afterTimeIndex)
				this.fetchDataToColumn('afterTimeIndex', '4,4')
				return resp.result.afterTimeIndex;
			},
			async initData() {
				//任教课程数据
				// let rjkc = await this.$http.get({
				// 	url: '/edu/classCourseChange/listCourseByTeacher'
				// });
				// this.column[6].range = this.column[0].range = rjkc.result.map(item => {
				// 	return {
				// 		name: item.courseName,
				// 		value: item.courseCode
				// 	};
				// });
				//任教班级数据
				let rjbj = await this.$http.get({
					url: '/edu/classCourseChange/listClassByTeacher'
				});
				this.column[5].range = rjbj.result.map(item => {
					return {
						name: item.className,
						value: item.classCode
					};
				});

			},
			async submitFun() {
				let user = await login.getStorage('user');
				let params = Object.assign(this.formModel, {
					changeType: 3
				})
				let resp = await this.$http.post({
					url: '/edu/wechat/classCourseChange/save',
					data: params
				});
				if (resp.code == 0) {
					uni.showToast({
						title: '提交成功',
						icon: 'none',
						success() {
							uni.navigateBack();
						}
					});
				}
			},

		},
		components: {
			baseForm
		},
		computed: {
			suspendDay() {
				let that = this
				if (this.column.length==0) return
				let start = this.column[0].value.replace(/-/g, '/');
				var startTime = new Date(start).getTime();
				let end = this.column[1].value.replace(/-/g, '/');
				var endTime = new Date(end).getTime();
				let suspendDay = endTime - startTime
				if (suspendDay <= 0) {
					uni.showToast({
						title: '结束时间不能小于或等于开始时间',
						icon: 'none',
						duration:2000
					})
					// this.column[1].value = ''
					that.column[1].text = '请选择'
					return ''
				} else {
					return suspendDay / 1000 / 60 / 60 / 24
				}
			}

		},
		watch: {
			column: {
				async handler(n, o) {
					if (n[4].value == o[4].value) return;
					let resp = await this.getInfo(1, '', n[4].value, n[3].value);
					console.log(resp)
					this.column[5].range = resp.map(item => {
						return {
							name: item.className,
							value: item.classCode
						};
					});
				},
				deep: true
			},
			suspendDay(n) {
				if (!n) {
					this.column[1].value = ''
					this.column[1].text = '请选择'
				}
				this.column[2].value = n
			}
		}
	};
</script>

<style></style>
